#include "process.h"
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

// 函数指针类型
typedef void (*call_t)(char*, double, double);


double total = 1024.0;
double speed[] = {1.0, 0.5, 0.3 , 0.2, 0.01, 0.01};

// 回调函数
void download(double total, call_t cb)
{
    srand(time(NULL));
    double current = 0.0;

    while(current <= total)
    {
        cb("下载中", total, current); // 进行回调
        if(current >= total) break;

        // 下载代码
        int random = rand() % 6;
        usleep(5000);
        //current += speed;
        current += speed[random];
        
        if(current >= total) current = total;
    }

    printf("download %.1lfMB done\n", total);
}

void upload(double total, call_t cb)
{
    srand(time(NULL));
    double current = 0.0;

    while(current <= total)
    {
        cb("上传中", total, current); // 进行回调
        if(current >= total) break;

        // 下载代码
        int random = rand() % 6;
        usleep(5000);
        //current += speed;
        current += speed[random];
        
        if(current >= total) current = total;
    }

    printf("upload %.1lfMB done\n", total);
}

int main()
{
    download(1024.0, FlushProcess);
    download(512.0, FlushProcess);
    download(256.0, FlushProcess);
    download(128.0, FlushProcess);
    download(54.0, FlushProcess);


    upload(54.0, FlushProcess);
    upload(54.0, FlushProcess);


    return 0;

}
